#include"file.h"


//====================================
void  gettime(char _time[])
{
     time_t time3;
     struct tm *p;
     time3 = time(NULL);
    
    p = localtime(&time3);
   
    sprintf(_time,"%04d-%02d-%02d %02d-%02d-%02d", (1900 + p->tm_year), ( 1 + p->tm_mon), p->tm_mday,
                                p->tm_hour, p->tm_min, p->tm_sec);

}

//创建文件
void file_create(char filename[])
{
    
    int fd;
    char _filename[100] = "/mnt/hgfs/git/";  
    strcat(_filename,filename);
    strcat(_filename,"/");
    strcat(filename,".txt");
    strcat(_filename,filename);
     printf("%s\n",_filename);
    fd = open(_filename,O_RDWR|O_CREAT,0777);
    if(fd<0)
    {
        printf("open%serror:%s",_filename,strerror(errno));
    }
    int  ret = write(fd, "hello world",12);
    if (-1 == ret) 
    {
        printf("Error: write file failed!\n");

    }
    close(fd);
    
 }

/*
    创建文件,与dir_name有关
*/
void file_create_dir(char filename[])
{
    
    int fd;
    strcat(filename,".txt");
    strcat(dir_name,filename);
     printf("%s\n",dir_name);
    fd = open(dir_name,O_RDWR|O_CREAT,0777);
    if(fd<0)
    {
        printf("open%serror:%s",dir_name,strerror(errno));
    }
    int  ret = write(fd, "hello world",12);
    if (-1 == ret) 
    {
        printf("Error: write file failed!\n");

    }
    close(fd);
    
 }

//====================================
int dir_create(char filename[])
{
    memset(dir_name,0,100);
   sprintf(dir_name, "%s", DIR_PATH);
    strcat(dir_name,filename);

    int fd = mkdir(dir_name,0777);
    if(fd == -1)
    {
        printf("mkdir error\r\n");
        return -1;
    }
    if(fd == 0)
    {
        printf("mkdir ok\r\n");
        strcat(dir_name,"/");
        return 0;
    }
    
}


//创建文件
void create_comtrade_HDR(char* filename)
{      
    char path[100];
    strcpy(path,dir_name);
    strcpy(a.HDR_name,filename);

    strcat(a.HDR_name,".hdr");

    strcat(path,a.HDR_name);
     printf("%s\n",path);
    a.HDR = open(path,O_RDWR|O_CREAT,0777);
    if(a.HDR<0)
    {
        printf("open%serror:%s",path,strerror(errno));
    }
    // int  ret = write(a.HDR, "hello world",12);
    // if (-1 == ret) 
    // {
    //     printf("Error: write file failed!\n");

    // }
    //close(a.HDR); 
 }
//=====================================
 //创建文件
void create_comtrade_CFG(char* filename)
{      
    char path[100];
    strcpy(path,dir_name);
    strcpy(a.CFG_name,filename);

    strcat(a.CFG_name,".cfg");

    strcat(path,a.CFG_name);
     //printf("%s\n",path);
    a.CFG = open(path,O_RDWR|O_CREAT,0777);
    if(a.CFG<0)
    {
        printf("open%serror:%s",path,strerror(errno));
    }
    // int  ret = write(a.CFG, "hello world",12);
    // if (-1 == ret) 
    // {
    //     printf("Error: write file failed!\n");

    // }
    //close(a.CFG); 
 }
//=====================================
 void create_comtrade_DAT(char* filename)
{      
    char path[100];
    strcpy(path,dir_name);
    strcpy(a.DAT_name,filename);

    strcat(a.DAT_name,".dat");

    strcat(path,a.DAT_name);
     //printf("%s\n",path);
    a.DAT = open(path,O_RDWR|O_CREAT,0777);
    if(a.DAT<0)
    {
        printf("open%serror:%s",path,strerror(errno));
    }
    // int  ret = write(a.DAT, "hello world",12);
    // if (-1 == ret) 
    // {
    //     printf("Error: write file failed!\n");

    // }
    //close(a.DAT); 
 }
  //====================================
  void create_comtrade_INF(char* filename)
  {      
    char path[100];
    strcpy(path,dir_name);
    strcpy(a.INF_name,filename);

    strcat(a.INF_name,".inf");

    strcat(path,a.INF_name);
     printf("%s\n",path);
    a.INF = open(path,O_RDWR|O_CREAT,0777);
    if(a.INF<0)
    {
        printf("open%serror:%s",path,strerror(errno));
    }
    // int  ret = write(a.INF, "hello world",12);
    // if (-1 == ret) 
    // {
    //     printf("Error: write file failed!\n");
    // }
    // close(a.INF); 
 }

/*
    测试程序，将dat文件内的数据读出，放到结构体内，以便进行数据文件写测试
    data_do    处理读出的字符串
    raed_file  读文件
*/

